﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GomeeSoft.src.classes;

namespace GomeeSoft.src.classes.DiaryPages
{
    public partial class UCDayPanel : Panel
    {
        // Encapsulation ******************************************************************
        #region Encapsulation
        private DateTime date;
        public DateTime Date
        {
            get { return date; }
            set { date = value; SetDate(); Invalidate(); }
        }
        private DateType dateType;
        public DateType DateType
        {
            get { return dateType; }
            set { dateType = value; PaintByDateType(); }
        }
        private bool isCurrentMonth;
        public bool IsCurrentMonth
        {
            get { return isCurrentMonth; }
            set { isCurrentMonth = value; PaintIsCurrentMonth();  }
        }
        private Label LBDate;
        private Label LBDateName;
        private Label LBLunarDate;
        private Label[] LBItems;
        #endregion

        // Initialize *********************************************************************
        #region Initialize
        public UCDayPanel()
        {
            InitializeComponent();
            InitializeLabel();
        }
        private void InitializeLabel()
        {
            LBDate = new Label();
            LBDate.AutoSize = true;

            LBDateName = new Label();
            LBDateName.AutoSize = false;
            LBDateName.AutoEllipsis = true;

            LBLunarDate = new Label();
            LBLunarDate.AutoSize = false;
            LBLunarDate.AutoEllipsis = true;
            LBLunarDate.Anchor = AnchorStyles.Bottom & AnchorStyles.Left & AnchorStyles.Top;
            LBLunarDate.TextAlign = ContentAlignment.BottomLeft;
            LBLunarDate.ForeColor = Global.GetColor(Global.MonthPageLunarDateForeColor);

            Controls.Add(LBDate);
            Controls.Add(LBDateName);
            Controls.Add(LBLunarDate);

            LBDateName.Visible = false;
            LBLunarDate.Visible = false;
        }
        #endregion
        
        // Paint **************************************************************************
        #region Paint
        private void UCDayPanel_Paint(object sender, PaintEventArgs e)
        {
            PaintByDateType();
        }
        private void PaintByDateType()
        {
            switch (DateType)
            {
                case DateType.Holiday:
                    BackColor = Global.GetColor(Global.MonthPageDateBackColorSunday);
                    LBDate.ForeColor = Global.GetColor(Global.MonthPageDateForeColorSunday);
                    LBDateName.ForeColor = Global.GetColor(Global.MonthPageDateForeColorSunday);
                    break;
                case DateType.Sunday:
                    BackColor = Global.GetColor(Global.MonthPageDateBackColorSunday);
                    LBDate.ForeColor = Global.GetColor(Global.MonthPageDateForeColorSunday);
                    break;
                case DateType.Saturday:
                    BackColor = Global.GetColor(Global.MonthPageDateBackColorSaturday);
                    LBDate.ForeColor = Global.GetColor(Global.MonthPageDateForeColorSaturday);
                    break;
                case DateType.Workday:
                default:
                    BackColor = Global.GetColor(Global.MonthPageDateBackColorWorkday);
                    LBDate.ForeColor = Global.GetColor(Global.MonthPageDateForeColorWorkday);
                    break;
            }

            if (Date.DayOfWeek == DayOfWeek.Sunday)
            {
                SetLunarDate();
            }
        }
        private void PaintIsCurrentMonth()
        {
            if (IsCurrentMonth)
            {
                LBDate.Font = new Font(LBDate.Font.FontFamily, 9, FontStyle.Bold);
            }
            else
            {
                LBDate.Font = new Font(LBDate.Font.FontFamily, 8);
            }
        }
        #endregion

        // Resize *************************************************************************
        #region Resize
        private void UCDayPanel_Resize(object sender, EventArgs e)
        {
            LBDateName.Left = LBDate.Left + LBDate.Width;
            LBDateName.Width = Width - LBDateName.Left;
            LBLunarDate.Top = Height - LBLunarDate.Height;
            LBLunarDate.Width = Width;
        }
        #endregion

        // Business Logic *****************************************************************
        #region Business Logic
        public void SetDate()
        {
            LBDate.Text = Date.Day.ToString();
            this.DateType = Calendar.GetDateType(Global.country, Date);
            if (this.DateType == DateType.Holiday)
            {
                LBDateName.Text = Calendar.GetHolidayName(Date);
                LBDateName.Visible = true;
            }
            else
            {
                LBDateName.Visible = false;
            }
        }
        public void SetLunarDate()
        {
            Calendar.LunarDate lunarDate = Calendar.ToKoreanLunarDate(Date);
            LBLunarDate.Text = lunarDate.lunMonth + "/" + lunarDate.lunDay;
            LBLunarDate.Visible = true;
        }
        #endregion
    }
}
